package com.mobile.controller;

import com.mobile.domain.dto.ChatDTO;
import com.mobile.service.MobileChatService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/chat")
@RequiredArgsConstructor
@Slf4j
public class ChatController {

    private final MobileChatService mobileChatService;
    private final VectorStore vectorStore;

    /**
     * ChatClient 简单调用
     */
    @PostMapping(value = "stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> simpleChat(@RequestBody ChatDTO chatDTO) {
        return mobileChatService.streamChat(chatDTO);
    }

    /**
     * 搜索向量数据库
     */
    @PostMapping("/search")
    public List<Document> search(@RequestParam("query") String query) {
        return vectorStore.similaritySearch(SearchRequest.builder()
                        .query(query)
                        .topK(3)
                        .build());
    }
}

